﻿using System;
using System.Data;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace VMExplorer.MemoryObjects
{
	partial class EnvironmentBlockTab : UserControl
	{
		public EnvironmentBlockTab (IMemoryObject mo)
		{
			InitializeComponent ();

			IntPtr byteBuffer = Marshal.AllocHGlobal((int)mo.Ranges[0].Size);
			if (RemoteReader.ReadMemory (mo.ProcessHandle, mo.Ranges [0].Base, byteBuffer, mo.Ranges [0].Size))
			{
				IntPtr curBuffer = byteBuffer;
				while (true)
				{
					string envVariable = Marshal.PtrToStringUni (curBuffer);
					if (envVariable == string.Empty) break;

					listView1.Items.Add (new ListViewItem (envVariable.Split (new char [] { '=' }, 2, StringSplitOptions.RemoveEmptyEntries)));

					curBuffer = (IntPtr)(curBuffer.ToInt32 () + 2 * (envVariable.Length + 1));
				}
			}

			listView1.Columns [0].Width = -2;
			listView1.Columns [1].Width = -2;
		}
	}
}
